package com.song.tools.songtest.service;

import com.song.tools.mvc.service.AggregationService;
import com.song.tools.mvc.validator.StrNotBlankValidator;
import com.song.tools.mvc.validator.ValidationResult;
import com.song.tools.mvc.validator.ValidatorChain;
import com.song.tools.songtest.beans.poi.SysUserImportDto;
import com.song.tools.songtest.beans.request.SysUserQueryRequest;
import com.song.tools.songtest.beans.request.SysUserSaveRequest;
import com.song.tools.songtest.beans.request.SysUserUpdateRequest;
import com.song.tools.songtest.beans.response.SysUserDeleteResponse;
import com.song.tools.songtest.beans.response.SysUserQueryResponse;
import com.song.tools.songtest.beans.response.SysUserSaveResponse;
import com.song.tools.songtest.beans.response.SysUserUpdateResponse;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * <p>
 * 用户表 服务类
 * </p>
 *
 * @author song tools
 * @since 2024-07-09
 */
public interface SysUserService extends AggregationService<Long, SysUserQueryRequest, SysUserQueryResponse,
        SysUserSaveRequest, SysUserSaveResponse, SysUserUpdateRequest, SysUserUpdateResponse, SysUserDeleteResponse, HttpServletResponse> {

    @Override
    default ValidationResult validateOnSave(SysUserSaveRequest sysUserSaveRequest) {
        ValidatorChain validatorChain = new ValidatorChain();

        // save时校验参数
        validatorChain.add(() -> new StrNotBlankValidator("loginAccount").validate(sysUserSaveRequest::getLoginAccount));
        validatorChain.add(() -> new StrNotBlankValidator("password").validate(sysUserSaveRequest::getPassword));
        validatorChain.add(() -> new StrNotBlankValidator("username").validate(sysUserSaveRequest::getUsername));
        return validatorChain.validate();
    }


    @Override
    default ValidationResult validateOnUpdate(SysUserUpdateRequest sysUserUpdateRequest) {
        ValidatorChain validatorChain = new ValidatorChain();

        // update时校验参数
        return validatorChain.validate();
    }


    /**
     * 导入前的数据校验
     *
     * @param importDtos 导入dto
     */
    default ValidationResult validateOnImport(List<SysUserImportDto> importDtos) {
        for (SysUserImportDto importDto : importDtos) {
            ValidatorChain validatorChain = new ValidatorChain();

            // import时校验参数
            validatorChain.add(() -> new StrNotBlankValidator("loginAccount").validate(importDto::getLoginAccount));
            validatorChain.add(() -> new StrNotBlankValidator("password").validate(importDto::getPassword));
            validatorChain.add(() -> new StrNotBlankValidator("username").validate(importDto::getUsername));
            ValidationResult validationResult = validatorChain.validate();
            if (!validationResult.isValid()) {
                return validationResult;
            }
        }
        return ValidationResult.success();
    }
}

